FOR and NEXT (Command)
V1 Indications are, FOR and NEXT in SmileBasic V1 are identical to those in V2. V2 FOR and NEXT are commands that cause the code between them to be run one or more times. FOR is followed by a variable name, the = symbol, a numerical expression, the TO keyword, and another numerical expression (and optionally, the STEP keyword and a third numerical expression). NEXT is either a complete command by itself, or it is followed by a variable name. Most commonly, it is used like the following: FOR I=1 TO 10 ' Code to be repeated 10 times NEXT I The interior of the loop is executed once with the variable I containing the value 1, then it is executed once with I equal to 2, etc., and on its final loop I is equal to 10. If the interior of the loop were PRINT I, the loop as a whole would print all the integers from 1 to 10 inclusive. This makes it easy to initialize an array: ' I want an array with 20 elements. DIM A(20) ' Right after DIM, all elements are essentially initialized to 0. ' I want them all initialized to the value 5. FOR I=0 TO 19 A(I)=5 NEXT I ' The array is now initialized with all elements equal to 5. If you wish to 'count by' some value other than 1, use the STEP option: FOR A=0 TO 10 STEP 2 'Inside this loop, A will have the values 0, 2, 4, 6, 8, 10 NEXT A FOR B=0 TO 2 STEP 0.5 ' Inside this loop, B will have the values 0, 0.5, 1, 1.5, 2 NEXT B It can also count down, using a negative STEP value: FOR X=4 TO -4 STEP -2 ' Inside this loop, X will have the values 4, 2, 0, -2, -4 NEXT X Technically, when a FOR is encountered, the location in program memory is added to a stack, the expression before TO is evaluated, and that value is assigned to the variable. When a NEXT is encountered, and there is a variable name after it, the variable name at the FOR at the top of the stack is compared against it. If there is a mismatch of variable names, the system generates a FOR without NEXT (FOR) error. If there is no variable name mismatch, the expression after TO is evaluated, then the expression after STEP is evaluated (or if there is no STEP, the value 1 is used). The STEP value is added to the variable. If the STEP value is zero or positive and the variable is not greater than the TO value, the program goes back to the command just after the FOR. If the STEP value is negative and the variable is not less than the TO value, the program also goes back to the command just after the FOR. If neither of these conditions apply, the top of the stack is removed, and the program continues at the command after the NEXT. It is worth noting that the variable will always contain the value before TO at least once, at the beginning of the first loop, but there is no guarantee it will ever contain the value after TO: e.g. FOR INT=1 TO 5.5, FOR ODD=1 TO 10 STEP 2. It is also worth commenting on the consequences of the TO and STEP expressions being evaluated each loop. Evaluating the TO each time means the following code: B=5 FOR A=1 TO B PRINT A B=B+0.5 NEXT A will not print 5 times, as you might expect from just looking at the first two lines; it will print 9 times. Evaluating the STEP each time allows for a neat structure rather like a REPEAT loop - where another dialect of BASIC might have REPEAT code UNTIL condition SmileBasic can have FOR REPEAT=0 TO 0 STEP condition code NEXT REPEAT At the end of each loop, condition is evaluated, and if it has the boolean value FALSE (i.e. 0), the loop will run again, and if condition evaluates to TRUE (i.e. 1), the loop ends. Of course, nested loops usually must have unique variable names. For instance, consider the code: FOR N=1 TO 10 code FOR N=1 TO 5 code NEXT N ' Point A NEXT N Unless N is otherwise modified in the inner loop, at Point A, N will always be 6, and the outer loop will never terminate. The stack space is only 255 elements deep, so at the very beginning of a program, you may have 255 consecutive FORs, but the 256th will cause an Out of memory (FOR) error. If a NEXT is encountered when the FOR-stack is empty, the system will generate a NEXT without FOR (NEXT) error. The FOR stack memory is shared with the GOSUB stack memory... so, if the very start of a program is a GOSUB, you can only run 254 consecutive FORs within that subroutine before getting an Out of memory error. Any additions to the FOR stack made in a subroutine, that are not removed in the normal way by the execution of a NEXT ending the loop, are removed by the RETURN. So, a single RETURN can free up two or more spaces in the stack memory. In contrast, additions to the call stack made during a FOR loop are not removed by a NEXT. V3 No information on SmileBasic V3. Category:Resources Category:System Guides Category:Commands